package brands

import (
	"github.com/gin-gonic/gin"
	"mxshop-api/goods-web/api"
	"mxshop-api/goods-web/forms"
	"mxshop-api/goods-web/global"
	"mxshop-api/goods-web/proto"
	"net/http"
	"strconv"
)

/*
*
获取品牌列表
*/
func brandList(ctx *gin.Context) {
	global.GoodsSrvClient.BrandList(ctx, &proto.BrandFilterRequest{
		Pages:       1,
		PagePerNums: 20,
	})
}

/*
*
创建品牌
*/
func createBrand() {

}

/*
*
修改品牌
*/
func updateBrand() {

}

/*
*
修改品牌
*/
func deleteBrand() {

}

/*
*
获取某个分类下的所有品牌
*/
func GetCategoryBrandList(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
	}
	list, err := global.GoodsSrvClient.GetCategoryBrandList(ctx, &proto.CategoryInfoRequest{
		Id: int32(i),
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	result := make([]interface{}, 0)
	for _, value := range list.Data {
		resultMap := make(map[string]interface{}, 0)
		resultMap["id"] = value.Id
		resultMap["name"] = value.Name
		resultMap["logo"] = value.Logo
		result = append(result, resultMap)
	}
	ctx.JSON(http.StatusOK, result)
}

/*
  - {
    "total":10,
    "data":[{},{}]
    }

获取分类品牌列表，分页
*/
func CategoryBrandList(ctx *gin.Context) {
	list, err := global.GoodsSrvClient.CategoryBrandList(ctx, &proto.CategoryBrandFilterRequest{
		Pages:       1,
		PagePerNums: 10,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	res := make(map[string]interface{}, 0)
	res["total"] = list.Total
	results := make([]interface{}, 0)
	for _, value := range list.Data {
		resultMap := make(map[string]interface{}, 0)
		resultMap["id"] = value.Id
		resultMap["category"] = map[string]interface{}{
			"id":   value.Category.Id,
			"name": value.Category.Name,
		}
		resultMap["brand"] = map[string]interface{}{
			"id":   value.Brand.Id,
			"name": value.Brand.Name,
		}
		results = append(results, resultMap)
	}
	res["data"] = results
	ctx.JSON(http.StatusOK, res)
}

/*
*
关联分类和品牌
*/
func NewCategoryBrand(ctx *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	err := ctx.ShouldBindJSON(&categoryBrandForm)
	if err != nil {
		api.HandleValidatorError(ctx, err)
		return
	}
	categoryBrand, err := global.GoodsSrvClient.CreateCategoryBrand(ctx, &proto.CategoryBrandRequest{
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	resultMap := make(map[string]interface{}, 0)
	resultMap["id"] = categoryBrand.Id
	ctx.JSON(http.StatusOK, resultMap)
}

/*
*
修改分类和品牌关联关系
*/
func UpdateCategoryBrand(ctx *gin.Context) {
	categoryBrandForm := forms.CategoryBrandForm{}
	err := ctx.ShouldBindJSON(&categoryBrandForm)
	if err != nil {
		api.HandleValidatorError(ctx, err)
		return
	}
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	_, err = global.GoodsSrvClient.UpdateCategoryBrand(ctx, &proto.CategoryBrandRequest{
		Id:         int32(i),
		CategoryId: int32(categoryBrandForm.CategoryId),
		BrandId:    int32(categoryBrandForm.BrandId),
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.Status(http.StatusOK)
}

/*
*
删除分类和品牌关联关系
*/
func DeleteCategoryBrand(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
		return
	}
	_, err = global.GoodsSrvClient.DeleteCategoryBrand(ctx, &proto.CategoryBrandRequest{
		Id: int32(i),
	})
	ctx.JSON(http.StatusOK, "")
}
